package org.jinxing.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jinxing.dao.BooksCaseDAO;
import org.jinxing.model.BooksCase;
import org.jinxing.model.BooksCaseExample;
import org.jinxing.model.BooksInfo;
import org.jinxing.model.BooksInfoExample;
import org.jinxing.service.BooksService;
import org.jinxing.utils.JsonUtils;
import org.jinxing.utils.ModelUtils;
import org.jinxing.utils.RequestUtils;
import org.jinxing.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/bcase")
public class BooksCaseController {
	protected static String className = BooksService.class.getName();
	protected static Log logger = LogFactory.getLog(className);//日志对象
	
	@Autowired
	BooksCaseDAO booksCaseDAO;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request,HttpServletResponse response,Model model){
		logger.info("<|>"+className+"<|>index<|><|><|>");
		return "/bcase/index";
	}
	
	@RequestMapping("/getBooksCase")
	public @ResponseBody String getBooksCase(HttpServletRequest request,HttpServletResponse response){
		logger.info("<|>"+className+"<|>getBooksCase<|><|><|>");
		int pageNo = RequestUtils.getInt(request, "page", 1);
		int pageSize = RequestUtils.getInt(request, "pagesize", 0);
		
		BooksCaseExample example = new BooksCaseExample();
		if(pageSize > 0){
			example.setLimitClauseStart((pageNo-1)*pageSize);
			example.setLimitClauseCount(pageSize);
		}
		
		
		example.setOrderByClause(" id desc ");
		
		List<BooksCase> list = booksCaseDAO.selectByExample(example);
		int count = booksCaseDAO.selectCountByExample(example);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("Rows", list);
		result.put("Total", count);
		return JsonUtils.jsonFromObject(result);
	}
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody String saveOrUpdate(HttpServletRequest request,HttpServletResponse response){
		String json = request.getParameter("json");
		Map<String,Object> result = new HashMap<String,Object>();
		List<Integer> successId = new LinkedList<Integer>();
		List tempList = JsonUtils.listFromJson(json);
		for(Object obj : tempList){
			BooksCase booksCase = ModelUtils.booksCasefyFromJson(JsonUtils.jsonFromObject(obj));

			if(null == booksCase.getId()){
				Integer id = this.booksCaseDAO.insert(booksCase);
				if(0 < id){
					successId.add(id);
				}
			}else{
				if(this.booksCaseDAO.updateByPrimaryKey(booksCase) == 1){
					successId.add(booksCase.getId());
				}
			}
		}
		result.put("sucessList", successId);
		logger.info("<|>"+className+"<|>saveOrUpdate<|><|>"+successId+"<|>");
		return JsonUtils.jsonFromObject(result);
	}
}
